package com.subaiqiao.yunshiyuan.entrance.ysy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subaiqiao.yunshiyuan.basis.utils.Result;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.SysCategory;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.SysCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 曹朝禹
 * @since 2022-05-13
 */
@RestController
@RequestMapping("/ysy/sys-category")
@Api(tags = "系统类别信息")
public class SysCategoryController {

    @Autowired
    private SysCategoryService sysCategoryService;

    @ApiOperation("查询类别信息")
    @GetMapping("/FindSysCategoryPage.do")
    public Result FindPage(
            @RequestParam @ApiParam("页码") Integer pageNum,
            @RequestParam @ApiParam("数量") Integer pageSize,
            @RequestParam(defaultValue = "") @ApiParam("类型名称") String sysCategoryName,
            @RequestParam(defaultValue = "") @ApiParam("作用域") String sysCategoryScopeAction
    ){
        QueryWrapper<SysCategory> wrapper = new QueryWrapper<>();
        wrapper.like("sys_category_name",sysCategoryName)
                .like("sys_category_scope_action", sysCategoryScopeAction);
        IPage<SysCategory> page = new Page<>(pageNum, pageSize);
        return Result.success(sysCategoryService.page(page,wrapper));
    }

    @ApiOperation("查询类别详情")
    @GetMapping("/FindSysCategoryDetail.do")
    public Result FindSysCategoryDetail(@RequestParam @ApiParam("商品类型编号") Integer sysCategoryId){
        return Result.success(sysCategoryService.getById(sysCategoryId));
    }

    @ApiOperation("添加商品类型")
    @PostMapping("/InsertSysCategory.do")
    public Result InsertProductCategory(SysCategory entity){
        boolean result = false;
        if(entity.getSysCategoryId()==null) result = sysCategoryService.save(entity);
        else result = sysCategoryService.updateById(entity);
        if(result) return Result.success();
        return Result.error();
    }

    @ApiOperation("删除一个类别")
    @DeleteMapping("/DeleteSysCategory.do")
    public Result DeleteSysCategory(@RequestParam @ApiParam("商品类型编号") Integer sysCategoryId){
        try {
            return Result.success(sysCategoryService.removeById(sysCategoryId));
        }catch (
            DataAccessException e){
                return Result.error("当前存在子类型或正在被使用");
            }
    }

    @ApiOperation("查询作用域类别所有信息")
    @GetMapping("/FindSysCategoryAll.do")
    public Result FindSysCategoryAll( @RequestParam(defaultValue = "") @ApiParam("作用域") String sysCategoryScopeAction
    ){
        QueryWrapper<SysCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_category_scope_action", sysCategoryScopeAction);
        return Result.success(sysCategoryService.list(wrapper));
    }


    @ApiOperation("查询所有类别数量")
    @GetMapping("/FindSysCategoryCount.do")
    public Result FindSysCategoryCount(){
        return Result.success(sysCategoryService.FindSysCategoryCount());
    }


}
